package com.gurid.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gurid.service.edu.entity.Video;
import com.gurid.service.edu.feign.VodMediaService;
import com.gurid.service.edu.mapper.VideoMapper;
import com.gurid.service.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Gurid
 * @since 2024-04-29
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodMediaService vodMediaService;

    @Override
    public void removeMediaVideoById(String id) {

        log.warn("VideoServiceImpl：video id = " + id);
        //根据videoid找到视频id
        Video video = baseMapper.selectById(id);
        String videoSourceId = video.getVideoSourceId();
        log.warn("VideoServiceImpl：videoSourceId= " + videoSourceId);
        vodMediaService.removeVideo(videoSourceId);
    }

    @Override
    public void removeMediaVideoByChapterId(String chapterId) {
        ArrayList<String> videoSourceIdList = getVideoSourceIdList("chapter_id", chapterId);

        //调用接口删除所有视频
        vodMediaService.removeVideoByIdList(videoSourceIdList);
    }

    @Override
    public void removeMediaVideoByCourseId(String courseId) {
        ArrayList<String> videoSourceIdList = getVideoSourceIdList("course_id", courseId);

        //调用接口删除所有视频
        vodMediaService.removeVideoByIdList(videoSourceIdList);
    }

    /**
     * 组装视频id列表
     * @param filterIdName 查询视频id的column名，如chapter_id，course_id
     * @param filterIdValue 查询视频id的column值
     * @return videoSourceList 返回视频id列表
     */
    private ArrayList<String> getVideoSourceIdList(String filterIdName, String filterIdValue) {
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.select("video_source_id");
        videoQueryWrapper.eq(filterIdName, filterIdValue);

        //查询该课程下的所有视频id
        ArrayList<String> videoSourceIdList = new ArrayList<>();
        baseMapper.selectMaps(videoQueryWrapper).forEach((map) -> {
            String videoSourceId = map.get("video_source_id").toString();
            videoSourceIdList.add(videoSourceId);
        });
        return videoSourceIdList;
    }
}
